﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.DAO;

namespace Com.CMS263.BLL
{
    class ArtCateTransform:ITag
    {

        ArtTypeDAO typeDAO = new ArtTypeDAO();
        AttributeTypeBLL atbll = new AttributeTypeBLL();
        
        ArtTypeBLL abbll = new ArtTypeBLL();
        string link = null;
        string paraname = null;
        /// <summary>
        /// 要解析的含有标签的html代码
        /// </summary>
        private string ctx = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ctx">页面内容</param>
        /// 
        List<string> tags = new List<string>();
        List<string> pages = new List<string>();

        TagUtil tu = new TagUtil();

        ArtBaseBLL bll = new ArtBaseBLL();
        ExtValueBLL evbll = new ExtValueBLL();
        //public ArtCateTransform(string ctx)
        //{
        //    this.ctx = ctx;
        //}
        public int page = 0;
        public int count = 0;

        public int pagesize = 10;
        /// <summary>
        /// 得到替换标签后的内容
        /// </summary>
        /// <returns>返回替换后的结果</returns>
        public string Replace(string ctx)
        {

            //得到所有标签
            tags = tu.GetTag(ctx, "cms:cate", tags);
            if (tags.Count == 0)
            {
                return ctx;
            }

            foreach (string tag in tags)
            {
                //得到包含属性的标签字符串
                int end = tag.IndexOf(">");
                string arrtStr = tag.Substring(1, end - 1);
                //解析标签得到所有的属性
                Dictionary<string, string> attrDic = tu.Getattribute(arrtStr);
                List<string> menuidlist = new List<string>();
                int strmenuid = 0;
                if (!string.IsNullOrEmpty(attrDic["menuid"]))
                {
                    
                        strmenuid =Convert.ToInt32(attrDic["menuid"].Replace("\"", ""));
                        
                     
                        // menuID = int.Parse(str);
                    
                

                }
                //获取url属性
                link = attrDic["url"].Replace("\"", "");
                string showchild = attrDic["showchild"].Replace("\"", "");
                 paraname = attrDic["paraname"].Replace("\"","");
                ArtType at = new ArtType();
          
              // ab = abbll.GetArtBaseByMenuId(strmenuid);
                //at = abbll.GetArtTypeByPid(strmenuid);
                string tree = GetTypeTree(strmenuid);
                //查询出结果替换<cms:cate>
                ctx = ctx.Replace(tag, tree);
            }



            return ctx;
        }

        /// <summary>
        /// 得到树形结构的菜单
        /// </summary>
        /// <returns></returns>
        public string GetTypeTree(int id)
        {

            StringBuilder sb = new StringBuilder("<ul  >");
            IList<ArtType> list = typeDAO.GetArtTypeByPid(id);

            //循环生成菜单列表
            foreach (ArtType at in list)
            {

                //如果这个菜单的外部程序连接编号为0，那就找父类的外部程序连接
                string temp = "";
                if (at.outLink != 0)
                {
                    
                    if (link.IndexOf("?") > 0)
                    {
                       temp= link + "&" + paraname + "=" + at.id;
                    }
                    else
                    {
                        temp=link+ "?"+paraname+"=" + at.id ;
                    }
                }

                IList<ArtType> listarttype = new List<ArtType>();
                listarttype = typeDAO.GetArtTypeByPid(at.id);

                //大类是没有连接的，只是一个标题
                if (typeDAO.GetArtTypeByPid(at.id).Count > 0)
                {
                    sb.Append("<li ><a title='" + at.typeName + "'  id='" + at.id + "'   >" +at.typeName + "</a>");

                    sb.Append(GetTree(at.id, 0));
                }
                else
                {
                    foreach (ArtType item in listarttype)
                    {
                     
                       sb.Append("<li ><a title='" + at.typeName + "'  id='" + at.id + "'  >" + StringUtil.GetText(at.typeName, 20) + "</a>");
                        
                    }
                        sb.Append("<ul><li  class='Child'><a  title='" + at.typeName + "' href='" + temp + "'   id='" + at.id + "' >" + at.typeName + "</a></li></ul>");
                    
                  
                 
                }
                sb.Append("</li>");
            }

            sb.Append("</ul>");

            return sb.ToString();
        }
        public string GetTree(int Pid, int level)
        {

            StringBuilder sb = new StringBuilder("");
            IList<ArtType> list = typeDAO.GetArtTypeByPid(Pid);
            ArtType subType = typeDAO.GetArtTypeById(Pid);

            level++;

            //递归生成菜单
            //根据父类编号取得子类列表

            if (list.Count > 0)
            {
                sb.Append("<ul >");
                foreach (ArtType at in list)
                {

                    string temp = "";
                   
                    
                    if (link.IndexOf("?") > 0)
                    {
                        
                          temp=  link + "&"+paraname+"=" + at.id;
                       
                    }
                    else
                    {

                        temp=link + "?" + paraname + "=" + at.id;
                        
                    }

                    //判断外部程序情况

                  

                    if (typeDAO.GetArtTypeByPid(at.id).Count > 0)
                    {
                        sb.Append("<li  id='m" + at.id + "'>");

                        sb.Append("<a title='" + at.typeName + "'  id='" + at.id + "' >" + at.typeName + "</a>");
                    }
                    else
                    {
                        //foreach (ArtType item in list)
                        //{

                        //    if (typeDAO.GetArtTypeByPid(item.id).Count > 0)
                        //    {
                            sb.Append("<li  class='Child'  id='m" + at.id + "'>");
                        //    }
                        //}
                       

                        sb.Append("<a  href='" + temp + "' title='" + at.typeName + "' target='mainFrame'  id='" + at.id + "' >" +at.typeName + "</a>");
                    }
                    sb.Append(GetTree(at.id, level));


                    sb.Append("</li>");
                }
                sb.Append("</ul>");

            }


            return sb.ToString();
        }





    }
}
